package com.framework.workflow.service.impl;

import com.framework.workflow.constant.DataDict;
import com.framework.workflow.dto.NodeUser;
import com.framework.workflow.mapper.IdentityMapper;
import com.framework.workflow.service.IVariableService;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zzg
 */
@Service
public class VariableService implements IVariableService {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IdentityMapper identityMapper;

    @Override
    public HashMap<String, Object> getVariable(String procInstId, String taskId) {
        HashMap<String, Object> variable = getVariable(procInstId);
        if (StringUtils.isNotBlank(taskId)) {
            Map<String, Object> v2 = taskService.getVariables(taskId);
            if (null != v2) {
                variable.putAll(v2);
            }
        }

        // 插入流程默认全局变量
        if (variable.isEmpty()) {
            setCustomVariable(variable);
        }

        return variable;
    }

    @Override
    public HashMap<String, Object> getVariable(String procInstId) {
        HashMap<String, Object> variable = new HashMap<>(16);
        if (StringUtils.isNotBlank(procInstId)) {
            Map<String, Object> v1 = runtimeService.getVariables(procInstId);
            if (null != v1) {
                variable.putAll(v1);
            }
        }
        return variable;
    }

    /**
     * 获取下个节点配置的角色中的人员
     */
    @Override
    public List<String> usersByRole(List<String> groups) {
        List<String> users = new ArrayList<>();
        if (null == groups || groups.size() == 0) {
            return users;
        }
        List<NodeUser> userList = userNodesByRole(groups);
        return userList.stream().map(NodeUser::getId).collect(Collectors.toList());
    }

    @Override
    public List<NodeUser> userNodesByRole(List<String> groups) {
        List<NodeUser> users = new ArrayList<>();
        if (null == groups || groups.size() == 0) {
            return users;
        }
        return identityMapper.usersByRole(groups);
    }

    @Override
    public void saveNextTaskVariablesLocal(List<Task> tasks, Map<String, Object> variables) {
        tasks.forEach(t -> taskService.setVariablesLocal(t.getId(), variables));
    }

    @Override
    public List<NodeUser> userNodeByIds(List<String> idList) {
        return identityMapper.userNodeByIds(idList);
    }

    @Override
    public NodeUser userNodeById(String id) {
        List<String> idList = new ArrayList<>();
        idList.add(id);
        List<NodeUser> list = identityMapper.userNodeByIds(idList);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public NodeUser deptNodeById(String deptId) {
        return identityMapper.deptNodeById(deptId);
    }

    private void setCustomVariable(Map<String, Object> variable) {
        variable.put(DataDict.WfVariableKey.IS_REJECT.toString(), "0");
    }
}
